package com.wdd.atcrowdfunding.manager.service.impl;

import com.wdd.atcrowdfunding.bean.Role;
import com.wdd.atcrowdfunding.bean.User;
import com.wdd.atcrowdfunding.manager.dao.RoleMapper;
import com.wdd.atcrowdfunding.manager.dao.RolePermissionMapper;
import com.wdd.atcrowdfunding.manager.dao.UserRoleMapper;
import com.wdd.atcrowdfunding.manager.service.RoleService;
import com.wdd.atcrowdfunding.util.Page;
import com.wdd.atcrowdfunding.vo.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Override
    public Page<Role> queryRolePage(Map<String, Object> paramMap) {
        Page<Role> page = new Page<Role>((Integer) paramMap.get("pageno"),(Integer) paramMap.get("pagesize"));

        Integer startIndex = page.getStartIndex();
        paramMap.put("startIndex",startIndex);
        List<User> datas = roleMapper.queryList(paramMap);
        page.setDatas(datas);

        Integer totalsize = roleMapper.queryCount(paramMap);
        page.setTotalsize(totalsize);
        return page;
    }

    @Override
    public int deleteRole(Integer id) {
        userRoleMapper.deleteByRoleid(id);
        rolePermissionMapper.deleteByRoleid(id);
        return roleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Role getRoleById(Integer id) {
        return roleMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateRole(Role role) {
        return roleMapper.updateByPrimaryKey(role);
    }

    @Override
    public int deleteBatchRoleByVO(Data data) {
        userRoleMapper.deleteByVoRoleid(data);
        rolePermissionMapper.deleteByVoRoleid(data);
        return roleMapper.deleteBatchRoleByVO(data);
    }

    @Override
    public int saveRole(Role role) {
        return roleMapper.insert(role);
    }
}
